#coding=utf-8
import socket
import threading
import time
bind_ip = "127.0.0.1"
bind_port=8899

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((bind_ip,bind_port))
s.listen(5)
print "Waiting for connection..."

def tcplink(sock,addr):
    print "Accept new connection from %s:%s..."% addr
    sock.send('Welcome')
    while True:
        data =sock.recv(1024)
        time.sleep(1)
        if data== 'exit' or not data:
            break
        sock.send("hello,%s"  % data)
    sock.close()
    print "connection from %s:%s closed.   "% addr

while True:
    #接受一个新的连接
    sock,addr =s.accept()
    #创建新的线程来处理TCP连接
    t=threading.Thread(target=tcplink,args=(sock,addr))
    t.start()
